package com.changgou.item.service.Impl;

import com.alibaba.fastjson.JSON;
import com.changgou.common.vo.Result;
import com.changgou.goods.feign.CategoryFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Goods;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.item.service.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class PageServiceimpl implements PageService {

    @Autowired
    private SpuFeign spuFeign;

    @Autowired
    private CategoryFeign categoryFeign;

    @Autowired
    private TemplateEngine templateEngine;

    @Value("${com.changgou.pagepath}")
    private String pagepath;


    @Override
    public void createHtml(Long spuId) {

        try {
            //获取数据
            Map<String,Object> dataMap= buildDataMap(spuId);
            Context context = new Context();
            context.setVariables(dataMap);

            //创建输出对象
            String filename=pagepath+spuId+".html";
            File file = new File(filename);
            PrintWriter printWriter = new PrintWriter(file);

            /**
             * 渲染 = 模板 + 数据 + 输出对象
             * 参数1：模板名称
             * 参数2：数据
             * 参数3：输出对象
             */
            //输出
            templateEngine.process("item",context,printWriter);

            printWriter.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
    * 根据商品spu id生成静态页面
    * @param spuId 商品spu id
    */
    private Map<String, Object> buildDataMap(Long spuId) {
        Map<String,Object> dataMap=new HashMap<>();

        //根据商品spu id获取spu及sku列表
        Result<Goods> skuResultGoods = spuFeign.findGoodsBySpuId(spuId);

        //商品spu对象
        Spu spu = skuResultGoods.getData().getSpu();
        dataMap.put("spu",spu);

        //商品sku对象
        List<Sku> skuList = skuResultGoods.getData().getSkuList();
        dataMap.put("skuList",skuList);

        //商品三级分类
        dataMap.put("category1", categoryFeign.findById(Long.parseLong(spu.getCategory1Id()+"")).getData());
        dataMap.put("category2", categoryFeign.findById(Long.parseLong(spu.getCategory2Id()+"")).getData());
        dataMap.put("category3", categoryFeign.findById(Long.parseLong(spu.getCategory3Id()+"")).getData());

        /**
         * 将spu 图片字符串转换为集合对象
         */
        //商品图片列表
        if (!StringUtils.isEmpty(spu.getImages())){
            String spuImage = spu.getImages();
            String[] spuImageList = spuImage.split(",");
            dataMap.put("imageList",spuImageList);
        }

        /**
        * 将spu中的规格字符串转化为map；类似如下
        * {"机身内存":["64G","128G"]}
        */
        //商品规格
        Map specMap= JSON.parseObject(spu.getSpecItems(),Map.class);
        dataMap.put("specMap",specMap);

        return dataMap;
    }

    /**
     * 根据商品spu id删除静态页面
     * @param spuId 商品spu id
     * @return 操作结果
     */
    @Override
    public void deleteHtml(Long spuId) {
        //创建输出对象
        String filename=pagepath+spuId+".html";
        File file = new File(filename);
        if (file.exists()){
            file.delete();
        }
    }
}
